package com.runmi.webchat.aftersale.common.mail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.runmi.webchat.aftersale.common.utils.DateUtil;
import com.runmi.webchat.aftersale.common.utils.NumberUtil;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.dbcore.dao.SetupItemDao;
import com.runmi.webchat.aftersale.dbcore.dao.SetupOrderDao;
import com.runmi.webchat.aftersale.dbcore.dao.SetupOrderDetailDao;
import com.runmi.webchat.aftersale.dbcore.dao.UserOpenIdDao;
import com.runmi.webchat.aftersale.dbcore.entity.MailCc;
import com.runmi.webchat.aftersale.dbcore.entity.RegionInfo;
import com.runmi.webchat.aftersale.dbcore.entity.SetupItem;
import com.runmi.webchat.aftersale.dbcore.entity.SetupOrder;
import com.runmi.webchat.aftersale.dbcore.entity.SetupOrderDetail;
import com.runmi.webchat.aftersale.dbcore.entity.ShopInfo;
import com.runmi.webchat.aftersale.dbcore.mapper.MailCcMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.RegionInfoMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.SetupItemMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.SetupOrderDetailMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.SetupOrderMapper;
import com.runmi.webchat.aftersale.web.api.service.SetupService;

@Component
public class SetupMail {

protected static final Log log = LogFactory.getLog("logFile");
	
	@Autowired
	private SetupService setupService;
	
	@Autowired
	private SetupOrderDao setupOrderDao;
	
	@Autowired
	private SetupOrderMapper setupOrderMapper;
	
	@Autowired
	private SetupItemMapper setupItemMapper;
	
	@Autowired
	private RegionInfoMapper regionInfoMapper;
	
	@Autowired
	private UserOpenIdDao userOpenIdDao;
	
	@Autowired
	private SetupItemDao setupItemDao;
	
	@Autowired
	private MailCcMapper mailCcMapper;
	
	
	@Autowired
	private SetupOrderDetailMapper setupOrderDetailMapper;
	
	@Autowired
	private SetupOrderDetailDao setupOrderDetailDao;
	
	
	public void sendMailTo(SetupOrder setupOrder,List<SetupOrderDetail> orderItems,ShopInfo shopInfo){
		log.info("--------------------------sendMailTo:" +setupOrder.getCustomerName() + " orderId:" + setupOrder.getId());
		for(SetupOrderDetail orderItem:orderItems){
			SetupItem setupItem= setupItemMapper.selectByPrimaryKey(orderItem.getItemId());
			if(StringUtil.isNotEmptyStr(setupItem.getEmail())){
				String address =regionInfoMapper.selectByPrimaryKey(setupOrder.getProvinceId()).getRegionName();
				RegionInfo c= regionInfoMapper.selectByPrimaryKey(setupOrder.getCityId());
				if(null!=c){
					address=address + c.getRegionName();
				}
				RegionInfo area= regionInfoMapper.selectByPrimaryKey(setupOrder.getAreaId());
				if(null!=area){
					address=address +area.getRegionName();
				}
				address=address +setupOrder.getAddress();
				List<String> toAddrs=new ArrayList<String>();
				String[] addresss= setupItem.getEmail().split(";");
				for(String str:addresss){
					toAddrs.add(str);
				}
				//toAddrs.add(setupItem.getEmail());
				String html="尊敬的供应商：</br>";
				html=html + "以下为今日有生品见门店销售的贵司商品，烦请按顾客预留信息联系顾客上门安装。如有问题请联系相应门店工作人员，谢谢！";
				html=html + "<html><body><table border='1' cellspacing='0' >"
						+ "		<tr>"
						+ "		<th>订单时间</th>"
						+ "		<th>期望安装时间</th>"
						+ "		<th>订单编号</th>"
						+ "		<th>订单金额</th>"
						+ "		<th>客户姓名</th>"
						+ "		<th>客户电话</th>"
						+ "		<th>安装地址</th>"
						+ "		<th>商品名称</th>"
						+ "		<th>店长姓名</th>"
						+ "		<th>店长电话</th>"
						+ "		</tr>";
				html=html + "	<tr>"
						+ "		<td>"+ DateUtil.format(setupOrder.getCreateTime(), "MM.dd")+"</td>"
						+ "		<td>"+ DateUtil.format(setupOrder.getExpectDate(), "yyyy-MM-dd HH:mm")+"</td>"
						+ "		<td>"+ setupOrder.getOrderNo() +"</td>"
						+ "		<td>"+ NumberUtil.forPriceView(setupItem.getSalePrice())+"</td>"
						+ "		<td>"+ setupOrder.getCustomerName()+"</td>"
						+ "		<td>" + setupOrder.getMobile()+"</td>"
						+ "		<td>"+address+"</td>"
						+ "		<td>"+setupItem.getItemName()+"</td>"
						+ "		<td>"+shopInfo.getContactName()+"</td>"
						+ "		<td>"+shopInfo.getContactMobile()+"</td>"
						+ "		</tr>";
				html=html + "</table></body></html>";
				
				MailCc record=new MailCc();
				List<MailCc> mails= mailCcMapper.select(record);
				List<String> ccMail=new ArrayList<String>();
				if(null!=mails &&mails.size()>0){
					for(MailCc mail:mails){
						ccMail.add(mail.getCc());
					}
				}
				//String conteng=String.format("客户:%s提交的商品%s已经提交，请处理", setupOrder.getCustomerName(),setupItem.getItemName());
				log.info("--------------------"+setupItem.getEmail() + " content:" + html);
				try {
					SendMessage.sslHtmlSend(toAddrs,"有生品见商品安装需求",html,ccMail);
				} catch (AddressException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				try {
//					//
//				} catch (AddressException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (MessagingException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}
		}
	}
}
